package com.zn.task;

import com.zn.entity.AppCustomer;
import com.zn.entity.FutureReportOrder;
import com.zn.mapper.AppCustomerMapper;
import com.zn.mapper.FutureReportOrderMapper;
import com.zn.socket.SocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author YinJiaWei
 * @version 2018/8/24 13:38
 */
//@Component
public class CheckOrderRefTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(CheckOrderRefTask.class);
    @Value("${trade.socket.host}")
    private String host;
    @Value("${trade.socket.port}")
    private String port;
    @Autowired
    private FutureReportOrderMapper futureReportOrderMapper;
    @Autowired
    private AppCustomerMapper appCustomerMapper;

    //每天的凌晨3点 16点执行一次
//    @Scheduled(cron="0 0 03,16 * * ?")
    public void task(){
        try{
            SocketHandler socketHandler=SocketHandler.getInstance(host,Integer.valueOf(port));
            /**查询未得到c++回报的订单**/
            List<FutureReportOrder> futureReportOrderList=futureReportOrderMapper.selectNotRespCStatus();
            for(FutureReportOrder futureReportOrder:futureReportOrderList){
                boolean isClose=socketHandler.isServerClose();
                //如果断开
                if(isClose){
                    continue;
                }else{
                    AppCustomer appCustomer=appCustomerMapper.selectByPrimaryKey(futureReportOrder.getCustomerId());
                    //组装send给c++的数据 checkOrderRef|180824100001|a10001001
                    String sendDate="checkOrderRef|"+futureReportOrder.getOrderRef()+"|"+appCustomer.getCustomerTradeAccount();
                    socketHandler.sendMessage(sendDate);
                    LOGGER.info("execute checkOrderRefTask data............:" + sendDate);
                }
            }
        }catch (Exception e){
            LOGGER.error("CheckOrderRefTask Error ..............:" , e);
        }
    }

}
